<% include("../public/commonheader.html"){}%>
 <div class="x-nav">
          <span class="layui-breadcrumb">
            <a href="">首页</a>
            <a href="">管理员管理</a>
            <a>
              <cite>权限管理</cite></a>
          </span>
          <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()" title="刷新">
            <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i></a>
 </div>

<div class="layui-fluid">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md12">
                    <div class="layui-card">
                        <div class="layui-card-body ">

                        </div>
                        <div class="layui-card-header">
                            <button class="layui-btn layui-btn-danger" onclick="delAll()"><i class="layui-icon"></i>批量删除</button>
                            <button class="layui-btn" onclick="Sca.open('添加权限','${'/admin/privileges/add',url}',600,400)"><i class="layui-icon"></i>添加权限</button>
                        </div>
                        <div class="layui-card-body layui-table-body layui-table-main">
                            <table class="layui-table layui-form">
                                <thead>
                                  <tr>
								    <th width="64"><input type="checkbox" lay-filter="checkall" name="" lay-skin="primary" title="ID"/></th>
                                    <th>${L.caption}</th><th>${L.control}</th><th>${L.action}</th><th>${L.operation}</th>
								  </tr>
                                </thead>
                                <tbody class="x-cate">
								<%for(parent in parents){%>
                                  <tr cate-id='${lists[parent].privi_id!0}' fid='0'>
								    <td><input type="checkbox" name="id" value="${lists[parent].privi_id!0}" lay-skin="primary" title="${lists[parent].privi_id!0}"></td>
                                    <td><i class="layui-icon x-show" status='true'>&#xe623;</i>${lists[parent].caption!}</td><td>${lists[parent].control!}</td><td>${lists[parent].action!}</td>
                                    <td class="td-manage">
                                        <a title="编辑"  onclick="Sca.open('编辑','${'/admin/privileges/edit/privi_id/'+lists[parent].privi_id,url}',600,400)" href="javascript:;">
                                        <i class="layui-icon">&#xe642;</i>
                                      </a>
                                      <a title="删除" onclick="privilege_del(this,'${lists[parent].privi_id!0}')" href="javascript:;">
                                        <i class="layui-icon">&#xe640;</i>
                                      </a>
                                    </td>
                                  </tr>
								  <%if(lists[parent].child!=null){ for(child in lists[parent].child){%>
                                  <tr cate-id='${lists[child].privi_id!0}' fid='${lists[parent].privi_id!0}' >
								    <td><input type="checkbox" name="id" value="${lists[child].privi_id!0}" lay-skin="primary"></td>
                                    <td>&nbsp;&nbsp;&nbsp;&nbsp;${lists[child].caption!}</td><td>${lists[child].control!}</td><td>${lists[child].action!}</td>
									<td class="td-manage">
                                        <a title="编辑"  onclick="Sca.open('编辑','${'/admin/privileges/edit/privi_id/'+lists[child].privi_id,url}',600,400)" href="javascript:;">
                                        <i class="layui-icon">&#xe642;</i>
                                      </a>
                                      <a title="删除" onclick="privilege_del(this,'${lists[child].privi_id!0}')" href="javascript:;">
                                        <i class="layui-icon">&#xe640;</i>
                                      </a>
                                    </td>
                                  </tr>
								  <%}}}%>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div> 
<% include("../public/commonfooter.html"){}%>
<script>
      layui.use(['laydate','form'], function(){
	    $ = layui.jquery;
        var laydate = layui.laydate;
        var  form = layui.form;
       

        // 监听全选
        form.on('checkbox(checkall)', function(data){

          if(data.elem.checked){
            $('tbody input[name="id"]').prop('checked',true);
          }else{
            $('tbody input[name="id"]').prop('checked',false);
          }
          form.render('checkbox');
        }); 
        
        //执行一个laydate实例
        laydate.render({
          elem: '#start' //指定元素
        });

        //执行一个laydate实例
        laydate.render({
          elem: '#end' //指定元素
        });


      });

	   $(function(){
            $("tbody.x-cate tr[fid!='0']").hide();
            // 栏目多级显示效果
            $('.x-show').click(function () {
                if($(this).attr('status')=='true'){
                    $(this).html('&#xe625;'); 
                    $(this).attr('status','false');
                    cateId = $(this).parents('tr').attr('cate-id');
                    $("tbody tr[fid="+cateId+"]").show();
               }else{
                    cateIds = [];
                    $(this).html('&#xe623;');
                    $(this).attr('status','true');
                    cateId = $(this).parents('tr').attr('cate-id');
                    getCateId(cateId);
                    for (var i in cateIds) {
                        $("tbody tr[cate-id="+cateIds[i]+"]").hide().find('.x-show').html('&#xe623;').attr('status','true');
                    }
               }
            })
          })

          var cateIds = [];
          function getCateId(cateId) {
              $("tbody tr[fid="+cateId+"]").each(function(index, el) {
                  id = $(el).attr('cate-id');
                  cateIds.push(id);
                  getCateId(id);
              });
          }

/*用户-删除*/
      function privilege_del(obj,id){
          layer.confirm('子权限也会被同时删除，确认要删除吗？',function(index){
              //发异步删除数据
         $.post(jkas.url("/admin/privileges/drop"),{id:id},function(result)
			  {  if(200==result.code)
			     {
				  $(obj).parents("tr").remove();
                  layer.msg('已删除!',{icon:1,time:1000});
				  }
				  else
				  {layer.msg('删除出错!',{icon:5,time:1000});
				  }
			  },"json");
              
          });
      }



      function delAll (argument) {
        var ids = [];

        // 获取选中的id 
        $('tbody input[name="id"]').each(function(index, el) {
            if($(this).prop('checked')){
               ids.push($(this).val())
            }
        });
  
        layer.confirm('子权限也会被同时删除，确认要删除所选吗？',function(index){
            //捉到所有被选中的，发异步进行删除
			$.post(jkas.url("/admin/privileges/drop"),{ids:ids.toString()},function(result)
			  {  if(200==result.code)
			     {
                    layer.msg('删除成功', {icon: 1});
                    $(".layui-form-checked").not('.header').parents('tr').remove();
				 }
				  else
				  {layer.msg('删除出错!',{icon:5,time:1000});
				  }
			  },"json");
        });
      }
</script>